﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using GUILayerWPF.FlightRoutingService;
using System.Text.RegularExpressions;
/// <summary>
/// The GUILayerWPF namespace.
/// </summary>
namespace GUILayerWPF
{
    /// <summary>
    /// Interaction logic for SeatBookingGUI.xaml
    /// </summary>
    public partial class SeatBookingGUI : Window
    {
        //Variabler initialization
        private static FlightRouteServiceClient client;
        private Travel travel;
        private Customer mainCustomer;
        private List<SeatBooking> bookings;

        public SeatBookingGUI(Travel _travel, Customer _mainCustomer)
        {
            InitializeComponent();
            travel = _travel;
            client = new FlightRouteServiceClient();
            mainCustomer = _mainCustomer;
            bookings = new List<SeatBooking>();
            FillFlights();
        }

        //Fill ComboBox
        private void FillSeatsCombos()
        {
            CboABC.SelectedItem = null;
            Cbo123.SelectedItem = null;
            CboABC.Items.Clear();
            Cbo123.Items.Clear();
            
            List<Seat> availableSeats = new List<Seat>();
            //her søges på en seatbooking det SKAL være en flightinstance
            foreach(Seat seat in client.SelectFreeSeats(ListFlightSelectedFlight()))
            {
                availableSeats.Add(seat);

            }
                        
            foreach(Seat seat in availableSeats)
            {
                string seatStr = seat.SeatNo;
                string numb; 
                string abc = seatStr.Substring(seatStr.Length - 1);
                if (seatStr.Length < 2)
                {
                    numb = seatStr.Substring(0, 2);
                }
                else
                {
                    numb = seatStr.Substring(0, 1);
                }
                
                if (!CboABC.Items.Contains(abc))
                {
                    CboABC.Items.Add(abc);
                }
                if (!Cbo123.Items.Contains(numb))
                {
                    Cbo123.Items.Add(numb);
                }
            }
        }
        //Fill Listbox
        private void FillFlights()
        {
            foreach (SeatBooking sb in travel.SeatBooks)
            {
                string retstring;
                retstring = "Rute: " + sb.FlightInstance.Flight.RouteId + ", Fra: " + sb.FlightInstance.Flight.Departure.AirportName + ", Til: " + sb.FlightInstance.Flight.Arrival.AirportName;
                listFlights.Items.Add(retstring);
            }
        }

        //Button select click
        private void BtnSelectSeat_Click(object sender, RoutedEventArgs e)
        {
            if (bookings.Count < travel.Persons.Count() * travel.SeatBooks.Count())
            {
                try
                {
                    //TODO: ToString skal ændres.
                    string seatNo = (string)Cbo123.SelectedItem + (string)CboABC.SelectedItem;
                    Seat seat = client.FindSeatBySeatNoAndPlaneNo(seatNo, ListFlightSelectedFlight().Plane.PlaneNo);
                    listSeats.Items.Add(seat.SeatNo + " Rute nr: " + ListFlightSelectedFlight().Flight.RouteId);
                    SeatBooking booking = new SeatBooking();
                    booking.Seat = seat;
                    //listFlights.Items.
                    booking.FlightInstance = ListFlightSelectedFlight();
                    bookings.Add(booking);
                }
                
                catch(NullReferenceException)
                {
                    MessageBox.Show("Sædet eller rejsen er ikke valgt.");
                }
                catch(Exception)
                {
                    MessageBox.Show("Det er ikke muligt at vælge det ønskede sæde på den valgte rejse.");
                }
                FillSeatsCombos();
            }
            else
            {
                MessageBox.Show("Sædeantallet må ikke overstige personantallet");
            }
        }

        //type cast string
        private FlightInstance ListFlightSelectedFlight()
        {
            try
            {
                FlightInstance flightinstance = travel.SeatBooks[listFlights.SelectedIndex].FlightInstance;
                return flightinstance;
            }
            catch(Exception)
            {
                MessageBox.Show("Der er ikke blevet valgt nogen rejse.");
                return null;
            }
            
        }

        //Button click
        private void BtnFinish_Click(object sender, RoutedEventArgs e)
        {
            int i;
            i = new int();

            if(travel.SeatBooks.ToList().Count > 0)
            {
                try
                {
                    travel.SeatBooks = bookings.ToArray();
                    CalculateTotalPrice();
                    i = client.InsertTravel(travel);
                    if (i > -1)
                    {
                        try
                        {
                            PaymentSite paysite = new PaymentSite(travel, mainCustomer);
                            paysite.Show();
                            this.Close();
                        }

                        catch (NullReferenceException)
                        {
                            MessageBox.Show("Kunne ikke afslutte, er der tilføjet sæder til rejsen?");
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("Rejsen blev ikke indsat i systemet, prøv igen");
                }
            }
        }

        //button remove click
        private void BtnRemoveSeat_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string seatNo = (string)Cbo123.SelectedItem + (string)CboABC.SelectedItem;
                Seat seat = client.FindSeatBySeatNoAndPlaneNo(seatNo, ListFlightSelectedFlight().Plane.PlaneNo);
                SeatBooking booking = client.FindSeatBookingByFlightAndSeat(ListFlightSelectedFlight(), seat);
                bookings.Remove(booking);
                listSeats.Items.Remove(listSeats.SelectedItem);
            }
            catch(NullReferenceException)
            {
                MessageBox.Show("Kunne ikke fjerne det valgte sæde(null)");
            }
            catch(Exception)
            {
                MessageBox.Show("Kunne ikke fjerne det valgte sæde");
            }
        }

        //detect mouse click
        private void ListFlights_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            FillSeatsCombos();
        }

        //Button abort Click
        private void BtnAbort_Click(object sender, RoutedEventArgs e)
        {
            CreateBooking createBooking = new CreateBooking(mainCustomer);
            createBooking.Show();
            this.Close();
        }
        private void CalculateTotalPrice()
        {
            double price = 0;
            foreach(SeatBooking sb in travel.SeatBooks)
            {
                price += sb.FlightInstance.Flight.Price;
            }
            travel.TotalPrice = price;
        }
    }
}
